package com.alibaba.moco;

import com.alibaba.moco.util.PropertiesUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis 数据池初始化
 * 
 * @author wb-lj240895
 */
public class RedisPoolInstance {

    private static JedisPool pool;
		//System.out.print(System.currentTimeMillis());
    private static String    REDISADDRESS  = "redis_address";
    private static String    REDISPROT     = "redis_prot";
    private static String    REDISDATABASE = "redis_database";
    private static String    REDISPASS     = "redis_pass";

    public static JedisPool getPool() {
        if (null != pool) {
            return pool;
        } else {
            return createRedisPool();
        }
    }

    private synchronized static JedisPool createRedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(32);
        return new JedisPool(config, PropertiesUtils.getAttribute(REDISADDRESS),
                             Integer.parseInt(PropertiesUtils.getAttribute(REDISPROT)),
                             500,
                             PropertiesUtils.getAttribute(REDISPASS),
                             Integer.parseInt(PropertiesUtils.getAttribute(REDISDATABASE)));
    }
    public static Jedis getRedisClient() {
        Jedis redis = getPool().getResource();
        
        return redis;
    }
}
